
#include "memory.h"

uint64_t user_size;

uint64_t kernel_end;

uint64_t PAGE_NUM  = (0x1 << PAGE_4K_OFFSET) / 8;

uint64_t MEMORY_NUM = MEMORY_SIZE >> PAGE_4K_OFFSET;

memory memory_s = {0};

uint64_t boot_mm_alloc(){
    for(uint32_t i = memory_s.kernel_num; i < MEMORY_NUM; ++i){
        if(memory_s.status[i] == 0){
            memory_s.status[i] = 1;
            return (i << PAGE_4K_OFFSET) + MEMORY_START;
        }
    }
}

void boot_mm_dealloc(uint64_t address){
    uint64_t i = (address - kernel_end) >> PAGE_4K_OFFSET;
    memory_s.status[i] = 0;
}

void boot_mm_init(){
    kernel_end = memory_read___kernel_end();

    memory_s.kernel_num = (kernel_end - MEMORY_START) >> PAGE_4K_OFFSET;

    for(uint64_t i = 0; i < memory_s.kernel_num; ++i){
        memory_s.status[i] = 1;
    }

    user_size = MEMORY_END - kernel_end;

    memory_s.user_num = user_size >> PAGE_4K_OFFSET;

    printk("kernel_end: %x\n", kernel_end);
    printk("user_size: %x\n", user_size);
    printk("kernel_num: %x\n", memory_s.kernel_num);
    printk("user_page_num: %d\n", memory_s.user_num);
}